<template>
    <div class="con">
        <div class="header">
            <img src="../assets/images/header.png" alt="">
            <div class="title">{{head.storeName}}</div>
            <div class="tip">请输入付款金额(元)</div>
            <dl>

                <dd>¥</dd>
                <dt><input type="number" placeholder="0.00" class="number" v-model="number"></dt>
                <div @click="clear()" class="delete">
                    <img src="../assets/images/delete.png" alt="">
                </div>
            </dl>

        </div>
        <div class="main">
            <p class="choose">付款选项</p>
            <div class="groupbtn">
                <button v-for="(item,index) in detail " :key="index" :class="{active:item.beDefault}" @click="choose(item)">{{item.fqNum}}期<br>

                </button>

            </div>
            <div class="detail" v-show="!nofq">
                <p class="title" v-show="show" @click="showDetail(item)">查看分期详情 <img src="../assets/images/off.png" alt=""></p>
                <p class="title" v-show="!show" @click="closeDetail()">查看分期详情 <img src="../assets/images/on.png" alt=""></p>
                <p class="info" v-show="!show">{{fqDetail}}</p>
            </div>
            <p class="info" v-show="nofq">全额付款 无分期</p>

            <input type="number" placeholder="输入营业员手机号（选填）" class="phone" v-model="phone">
            <input type="text" placeholder="添加备注（选填）" class="remark" v-model="remark">

        </div>
        <div style="height:80px"></div>
        <div class="btn" @click="payment()" :class="{ graybtn: isgraybtn }">
            确认并支付
        </div>
    </div>
</template>



<script>
import { hex_md5 } from "../assets/md5.js";

export default {
    name: "index",
    data() {
        return {
            show: true,
            active: false,
            item: "",
            nofq: false,
            number: "",
            fqDetail: "",
            remark: "",
            phone: "",
            vendorId: "",
            storeId: "",
            fqActiveTypeKey: "",
            key: "",
            head: {},
            detail: {},
            isgraybtn: false
        };
    },
    methods: {
        clear() {
            this.number = "";
        },
        choose(item) {
            this.item = item;
            this.nofq = false;
            this.detail.forEach((data, index) => {
                data.beDefault = false;
            });
            item.beDefault = true;
            this.show = true;
            if (item.fqType == "zfb") {
                this.nofq = true;
            }
        },
        showDetail(item) {
            if (this.number == "") {
                this.$Message.warning("请输入付款金额");
            } else {
                var timestamp = Date.parse(new Date());
                this.show = false;
                this.axios({
                    method: "post",
                    url: `${
                        this.siteUrl
                    }/h5/fqsulotion/compute.do?timestamp=${timestamp}`,
                    data: {
                        vendorId: this.vendorId,
                        storeId: this.storeId,
                        amount: this.number,
                        fqId: item.fqId,
                        fqDetailId: item.fqDetailId
                    }
                })
                    .then(response => {
                        if (response.data.success == 1) {
                            this.fqDetail = response.data.data.info;
                        } else {
                            this.fqDetail = response.data.message;
                        }
                    })
                    .catch(function(error) {
                        console.log(error);
                    });
            }
        },
        closeDetail() {
            this.show = true;
        },
        payment() {
            var timestamp = Date.parse(new Date());
            if (this.number == "") {
                this.$Message.warning("请输入付款金额");
            } else {
                this.isgraybtn = true;

                var hex = `amount=${this.number}&vendorId=${
                    this.vendorId
                }&storeId=${this.storeId}&fqId=${this.item.fqId}&fqDetailId=${
                    this.item.fqDetailId
                }&fqType=${
                    this.item.fqType
                }&timestamp=${timestamp}&signkey=8jmjhy65tgvfdew`;

                this.axios({
                    method: "get",
                    url: `${
                        this.siteUrl
                    }/h5/customer/pay/submit.do?appid=web.2018&key=${
                        this.key
                    }&timestamp=${timestamp}&amount=${this.number}&vendorId=${
                        this.vendorId
                    }&storeId=${this.storeId}&fqId=${
                        this.item.fqId
                    }&fqDetailId=${this.item.fqDetailId}&fqType=${
                        this.item.fqType
                    }&fqNum=${this.item.fqNum}&fqActiveTypeKey=${
                        this.fqActiveTypeKey
                    }&remark=${this.remark}&phone=${this.phone}&sign=${hex_md5(
                        hex
                    )}`
                    // data: {
                    //     amount: this.number,
                    //     vendorId: this.vendorId,
                    //     storeId: this.storeId,
                    //     fqId: this.item.fqId,
                    //     fqDetailId: this.item.fqDetailId,
                    //     fqType: this.item.fqType,
                    //     fqNum: this.item.fqNum,
                    //     fqActiveTypeKey: this.fqActiveTypeKey,
                    //     remark: this.remark,
                    //     phone: this.phone,
                    //     sign: hex_md5(hex)
                    // }
                })
                    .then(response => {
                        if (response.data.success == 1) {
                            var href = response.data.data.url;
                            window.location.href = href;
                            this.isgraybtn = false;
                        } else {
                            this.isgraybtn = false;
                            this.$Message.warning(response.data.message);
                        }
                    })
                    .catch(function(error) {
                        console.log(error);
                    });
            }
        }
    },
    mounted() {
        // 获取商户Id
        this.vendorId = this.$route.query.vendorId;

        //获取门店id
        this.storeId = this.$route.query.storeId;
        //获取金融产品id
        this.fqActiveTypeKey = this.$route.query.fqActiveTypeKey;
        //获取key
        this.key = this.$route.query.key;

        var timestamp = Date.parse(new Date());
        this.$Message.config({
            top: 200,
            duration: 1
        });

        var timestamp = Date.parse(new Date()) + 100;
        this.axios({
            method: "post",
            url: `${
                this.siteUrl
            }/h5/fqsulotion/activelist.do?appid=web.2018&timestamp=${timestamp}key=${
                this.key
            }`,
            data: {
                vendorId: this.vendorId,
                storeId: this.storeId,
                fqActiveTypeKey: this.fqActiveTypeKey
            }
        })
            .then(response => {
                console.log(response);
                if (response.data.success == 1) {
                    this.head = response.data.data.head;
                    this.detail = response.data.data.detail;

                    this.detail.forEach((item, index) => {
                        if (item.fqType == "zfb") {
                            item.fqNum = "不分";
                        }
                        if (item.beDefault == "true") {
                            this.item = item;
                        }
                        if (item.beDefault == "false") {
                            item.beDefault = false;
                        }
                    });
                } else {
                    this.$Message.warning(response.data.message);
                }
            })
            .catch(function(error) {
                console.log(error);
            });
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    width: 100%;
    min-height: 100vh;
    background: #fff;
}
.header {
    position: relative;
}
.header img {
    width: 100%;
}
.header .title {
    position: absolute;
    width: 80%;
    left: 10%;
    top: 4.5%;
    text-align: center;
    font-size: 16px;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.tip {
    position: absolute;
    top: 46%;
    color: #333;
    font-weight: 200;
    left: 7%;
    font-size: 14px;
}
.header dl {
    width: 86%;
    position: absolute;
    left: 7%;
    top: 60%;
    height: 50px;
}
/* 一键删除按钮 */
.header .delete {
    width: 24px;
    height: 24px;
    position: absolute;
    top: 13px;
    right: 10px;
    z-index: 999;
}
.header .delete img {
    width: 16px;
    height: 16px;
    margin-left: 4px;
    margin-top: 4px;
}
.header dl dd {
    float: left;
    width: 7%;
    height: 34px;
    font-size: 18px;
    padding-top: 10px;
}
.header dl dt {
    float: left;
    width: 80%;
}

.header dl dt input {
    font-size: 34px;
    height: 49px;
    width: 100%;
}
.number::-webkit-input-placeholder {
    color: #bebebe;
    font-size: 34px;
}
/* 主体部分 */
.main {
    width: 86%;
    margin-left: 7%;
}
.main .choose {
    font-size: 14px;
    font-weight: 200;
}

.groupbtn button {
    background: #fff;
    border: 1px solid #d8d8d8;
    color: #bebebe;
    height: 52px;
    width: 32%;
    margin-top: 15px;
    border-radius: 10px;
    margin-left: 2%;
}

.groupbtn button:nth-child(3n + 1) {
    margin-left: 0%;
}
.groupbtn .active {
    background: linear-gradient(to top right, #0baede, #0178c3);
    border: none;
    color: #fff;
}
/* 分期详情 */
.detail .title {
    font-size: 12px;
    font-weight: 200;
    margin-top: 20px;
    width: 110px;
}
.detail .title img {
    width: 22px;
    padding-top: 3px;
}
.detail .info {
    font-size: 14px;
    color: #333;
    margin-top: 15px;
}
.info {
    font-size: 14px;
    color: #333;
    margin-top: 15px;
}
.btn {
    background: #0b94de;
    width: 100%;
    height: 64px;
    line-height: 64px;
    text-align: center;
    color: #fff;
    font-size: 18px;
    position: fixed;
    bottom: 0px;
    left: 0px;
}
.phone {
    margin-top: 30px;
    width: 100%;
    height: 40px;
}
.remark {
    margin-top: 10px;
    width: 100%;
    height: 40px;
}
.remark::-webkit-input-placeholder,
.phone::-webkit-input-placeholder {
    color: #999;
    font-size: 14px;
    font-weight: 200;
}

.graybtn {
    opacity: 0.5;
}
</style>
